home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / NewHelp 1.0.1 / NewHelp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-31  |  5.7 KB  |  283 lines  |  [TEXT/CWIE]

  1. // NewHelp
  2. // version 1.0.1
  3. // by Ken Long <kenlong@netcom.com>
  4. // updated for CW7 on 951215
  5.  
  6. typedef struct {
  7.     short textRsrc, stylRsrc;
  8. }TopicSet;
  9.  
  10. typedef struct {
  11.     short numTopics;
  12.     TopicSet topicSets [1];
  13. } HelpList, *HelpListPtr, **HelpListHdl;
  14.  
  15. DialogPtr myDialog;
  16. HelpListHdl myHelpListH;
  17. ListHandle theList;
  18. short pagesize, numTopics;
  19. Handle textRes, numTopicsH;
  20. ControlHandle sBar;
  21. TEHandle theText;
  22. Boolean finished;
  23.  
  24.  
  25. pascal void ScrAction(ControlHandle control, short controlPart)
  26. {
  27.     short step, oldValue;
  28.  
  29.     if (controlPart == 0)
  30.         return;
  31.     
  32.     switch( controlPart )
  33.     {
  34.         case inUpButton:
  35.             step = -10;
  36.         break;
  37.         
  38.         case inDownButton:
  39.             step = 10;
  40.         break;
  41.             
  42.         case inPageUp:
  43.             step = -pagesize;
  44.         break;
  45.         
  46.         case inPageDown:
  47.             step = pagesize;
  48.         break;
  49.     }
  50.     if( controlPart != 0 )
  51.     {
  52.         oldValue = GetCtlValue(control);
  53.         SetCtlValue(control, oldValue + step);
  54.         TEScroll(0, oldValue - GetCtlValue(control), theText);
  55.     }
  56. }
  57.  
  58. void WindowScroll(Point thePt, WindowPtr theWindow)
  59. {
  60.     ControlHandle control;
  61.     short part, oldValue;
  62.     
  63.     GlobalToLocal(&thePt);
  64.     switch( FindControl(thePt, theWindow, &control) )
  65.     {
  66.         case inUpButton:
  67.         case inPageUp: 
  68.         case inDownButton:
  69.         case inPageDown: 
  70.             part = TrackControl(control, thePt, (ControlActionUPP) ScrAction);
  71.         break;
  72.             
  73.         case inThumb: 
  74.         {
  75.             oldValue = (**theText).viewRect.top - (**theText).destRect.top;
  76.             if( TrackControl(control, thePt, 0L) != 0 )
  77.                 TEScroll(0, oldValue - GetCtlValue(control), theText);
  78.         }
  79.     }
  80. }
  81.  
  82. void LoadText(short item)
  83. {
  84.     short inactive = 255;
  85.         
  86.     short max, textNum, stylNum;
  87.     Handle textRes, stylRes;
  88.     
  89.     TEDeactivate(theText);
  90.     TESetSelect(0, 32767, theText);
  91.     TEDelete(theText);
  92.     
  93.     (**theText).destRect = (**theText).viewRect;
  94.     
  95.     textNum = (*myHelpListH)->topicSets[item].textRsrc;
  96.     stylNum = (*myHelpListH)->topicSets[item].stylRsrc;
  97.     
  98.     if( (textNum != 0) & (stylNum != 0) )
  99.     {
  100.         textRes = GetResource('TEXT', textNum);
  101.         stylRes = GetResource('styl', stylNum);
  102.         HLock(textRes);
  103.         HLock(stylRes);
  104.         TEStylInsert(*textRes, SizeResource(textRes), (StScrpHandle) stylRes, theText);
  105.         HUnlock(stylRes);
  106.         HUnlock(textRes);
  107.     }
  108.     max = TEGetHeight((**theText).nLines, 0, theText) - pagesize;
  109.     if( max > 0 )
  110.         HiliteControl(sBar, activeFlag); 
  111.     else
  112.         HiliteControl(sBar, inactive);
  113.         
  114.     SetCtlValue(sBar, 0);
  115.     SetCtlMax(sBar, max);
  116.     TEActivate(theText);
  117. }
  118.  
  119. Boolean MyFilter(DialogPtr theDialog, EventRecord *theEvent, 
  120.                  short *itemHit)
  121. {
  122.     short iType, part;
  123.     Rect iBox;
  124.     Handle iHdl;
  125.     Point thePt;
  126.     ControlHandle control;
  127.     Boolean ignore;
  128.     Cell newCell;
  129.     long ticks;
  130.     
  131.     if (theEvent->what == mouseDown)
  132.     {
  133.         thePt = theEvent->where;
  134.         GlobalToLocal(&thePt);
  135.         part = FindControl(thePt, myDialog, &control);
  136.             
  137.         if (control == sBar)
  138.             WindowScroll(theEvent->where, myDialog);
  139.         else 
  140.             if ( control == (**theList).vScroll )
  141.                 ignore = LClick(thePt, theEvent->modifiers, theList);
  142.         else 
  143.             if (PtInRect(thePt, &(**theList).rView))
  144.             {
  145.                 ignore = LClick(thePt, theEvent->modifiers, theList);
  146.                 SetPt(&newCell, 0, 0);
  147.                 ignore = LGetSelect(true, &newCell, theList);
  148.                 LoadText(newCell.v);
  149.             }
  150.     }
  151.     
  152.     if (theEvent->what == updateEvt)
  153.     {
  154.         TEUpdate(&(**theText).viewRect, theText);
  155.         LUpdate(myDialog->visRgn, theList);
  156.     }
  157. }
  158.  
  159. pascal void MyItemProc(WindowPtr theDlg, short itemHit )
  160. {
  161.     Rect    iRect;
  162.     Handle    iHndl;
  163.     short    iType;
  164.  
  165.     GetDItem (theDlg, 1, &iType, &iHndl, &iRect );     // item #1
  166.     PenSize( 3,3 );
  167.     InsetRect( &iRect, -4,-4);
  168.     FrameRoundRect( &iRect, 16,16 );
  169. }
  170.  
  171. void CreateList()
  172. {
  173.     short i, iType;
  174.     Str255 topicStr;
  175.     Rect iRect, dataBounds;
  176.     Cell cSize, theCell;
  177.     Handle iHandle;
  178.     
  179.     GetDItem(myDialog, 3, &iType, &iHandle, &iRect);
  180.     FrameRect(&iRect);
  181.     InsetRect(&iRect, 1, 1);
  182.     SetPt(&cSize, iRect.right - iRect.left, 12);
  183.     SetRect(&dataBounds, 0, 0, 1, numTopics);
  184.     theList = LNew(&iRect, &dataBounds, cSize, 0, myDialog, false, false, false, true);
  185.     
  186.     //o DrawIt, hasGrow, hScroll, vScroll.
  187.     for (i = 1; i <= numTopics; i++) 
  188.     {
  189.         GetIndString(topicStr, 256, i);
  190.         SetPt(&theCell, 0, i - 1);
  191.         LSetCell((Ptr) &topicStr + 1, *topicStr, theCell, theList);
  192.     }
  193.     (**theList).selFlags = lOnlyOne;
  194.     SetPt(&theCell, 0, 0);
  195.     LSetSelect(TRUE, theCell, theList);
  196.  
  197.     //o Set drawing to True now that list is built.
  198.     LDoDraw(TRUE, theList); 
  199. }
  200.  
  201. void Help(short strRsrcNum, short hlpRsrcNum)
  202. {
  203.     GrafPtr savePort;
  204.     EventRecord theEvent;
  205.     Rect iBox, tBox;
  206.     short iType, itemHit, thePart;
  207.     Handle iHdl;
  208.     Point mLoc;
  209.  
  210.     GetPort(&savePort);
  211.     myDialog = GetNewDialog(1001, 0L, (WindowPtr)-1L);
  212.     SetPort(myDialog);
  213.  
  214.     GetDialogItem( myDialog, 5, &iType, &iHdl, &iBox );
  215.     SetDialogItem( myDialog, 5, iType, (Handle) MyItemProc, &iBox );
  216.     ShowWindow(myDialog);
  217.  
  218.     TextFont(monaco);
  219.     TextSize(9);
  220.  
  221.     numTopics = **(short **)GetResource('STR#', strRsrcNum);
  222.     myHelpListH = (HelpListHdl) (GetResource('HLP#', hlpRsrcNum));
  223.     HLock((Handle) (myHelpListH));
  224.     
  225.     CreateList ();
  226.     GetDItem(myDialog, 2, &iType, &iHdl, &tBox);
  227.     FrameRect(&tBox);
  228.     InsetRect(&tBox, 4, 2);
  229.     pagesize = tBox.bottom - tBox.top;
  230.     theText = TEStylNew(&tBox, &tBox);
  231.     HLock((Handle) (theText));
  232.     
  233.     GetDItem(myDialog, 7, &iType, &iHdl, &iBox);
  234.     sBar = NewControl(myDialog, &iBox, "\p", false,
  235.                       1, 1, 1, scrollBarProc, 0L);
  236.     ShowControl(sBar);
  237.     
  238.     LoadText(0);
  239.             
  240.     finished = false;
  241.  
  242.     do 
  243.     {
  244.         ModalDialog((ModalFilterUPP) MyFilter, &itemHit);    /* show dialog/get result */
  245.         switch (itemHit) 
  246.         {                    /* control hit */
  247.             case 1: 
  248.                 finished = true;
  249.             break;
  250.             
  251.             default:
  252.             break;
  253.         }
  254.     } while (!finished);
  255.  
  256.     HUnlock((Handle) (theText));
  257.     LDispose(theList);
  258.     DisposDialog(myDialog);
  259.     SetPort(savePort);
  260.     TextFont (0);
  261.     TextSize (12);
  262. }
  263.  
  264. void InitMacintosh(void)
  265. {
  266.     MaxApplZone();
  267.     
  268.     InitGraf(&qd.thePort);
  269.     InitFonts();
  270.     FlushEvents(everyEvent, 0);
  271.     InitWindows();
  272.     InitMenus();
  273.     TEInit();
  274.     InitDialogs(0L);
  275.     InitCursor();
  276. }
  277.  
  278. void main(void)
  279. {
  280.     InitMacintosh();
  281.     Help(256, 256);
  282. }
  283.